package com.sky.Sdk;

import com.sky.Protocol.MessageChannelHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.sql.Time;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by sun on 2017-06-16.
 */
public class ClientBootstrap {
    private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    private SocketChannel socketChannel;
    private String ip;
    private int port;
    private int readerIdleTime;

    public ClientBootstrap(String ip, int port) {
        this.ip = ip;
        this.port = port;
        this.readerIdleTime = 90;
    }

    public ClientBootstrap(String ip, int port, int readerIdleTime) {
        this.ip = ip;
        this.port = port;
        this.readerIdleTime = readerIdleTime;
    }

    public void connect() throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

        int n = 2;
        for(int i = 0;i<n;i++) {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            bootstrap.group(eventLoopGroup);
            bootstrap.remoteAddress(ip, port);
            bootstrap.handler(new MessageChannelHandler(this,readerIdleTime));


            try {
                ChannelFuture future = bootstrap.connect(ip, port).sync();

                future.addListener(new ClientConnectListener(this));

                if (future.isSuccess()) {
                    socketChannel = (SocketChannel) future.channel();

                    System.out.println("connect server success!");
                }

//                future.channel().closeFuture().sync();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
            } finally {
//                eventLoopGroup.shutdownGracefully();

                executor.execute(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    System.out.println("reconnect ----");
//                    try {
//                        connect();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }

                });
            }

        }

//        eventLoopGroup.shutdownGracefully();

        while(true) {
            TimeUnit.SECONDS.sleep(5);
        }

    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public void setSocketChannel(SocketChannel socketChannel) {
        this.socketChannel = socketChannel;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getReaderIdleTime() {
        return readerIdleTime;
    }

    public void setReaderIdleTime(int readerIdleTime) {
        this.readerIdleTime = readerIdleTime;
    }
}
